package com.atxiaodei.nio.nioday01;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * @author wangmeng
 * @date 2020/9/19
 **/
public class Jdk8ForEach {

    // 方法1
    @Test
    public void test1(){
        List<String> list = Arrays.asList("Monkey", "Lion", "Giraffe", "Lemur", "Lion");

        list.forEach(System.out::println);
    }
    // 方法2
    @Test
    public void test2(){
        List<String> list = Arrays.asList("Monkey", "Lion", "Giraffe", "Lemur", "Lion");

        Consumer<String> action = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s.toUpperCase());
            }
        };
        list.forEach(action);
        Consumer<String> action1=s -> System.out.println(s.toUpperCase());
        list.forEach(action);

    }
    // 遍历map
    @Test
    public void test3(){
        Map<String, Integer> codes = new HashMap<>();
        codes.put("United States", 1);
        codes.put("Germany", 49);
        codes.put("France", 33);
        codes.put("China", 86);
        codes.put("Pakistan", 92);
        // 1
        codes.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println("key="+s+",value="+integer);
            }
        });
        System.out.println("-----------------------------------------");
        // 2
        codes.forEach((x,y)->System.out.println("key="+x+",value="+y));
        System.out.println("-----------------------------------------");
        // 3
        BiConsumer<String, Integer> action2 = new BiConsumer<String, Integer>() {
            @Override
            public void accept(String x, Integer y) {
                System.out.println("key="+x+",value="+y);
            }
        };
        codes.forEach(action2);
        System.out.println("-------------------key----------------------");
        // 只遍历map的key
        codes.keySet().forEach(System.out::println);
        System.out.println("-------------------value----------------------");
        // 只遍历map的value
        codes.values()
                .forEach(System.out::println);


    }

}
